package com.artifact.mit.mypoint.fragment;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;

import com.artifact.mit.mypoint.R;
import com.artifact.mit.mypoint.activity.ProductListActivity;
import com.artifact.mit.mypoint.activity.MyPointBaseActivity;
import com.artifact.mit.mypoint.adapter.MenuAdapter;
import com.artifact.mit.mypoint.model.MenuItem;
import com.artifact.mit.mypoint.model.api.Catalogue.CatalogueDetail;

public class NestedMenuFragment extends ListFragment {
	
	Map<String, ArrayList<CatalogueDetail>> map;
	int index = 0;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.left_nav_menu_contents, null);

	}
	
	private class MenuAsyncTask extends AsyncTask<String, Void, MenuAdapter>{

		@Override
		protected MenuAdapter doInBackground(String... arg0) {
			
			try{
				MenuAdapter adapter = new MenuAdapter(getActivity());
				ContextWrapper cw = new ContextWrapper(getActivity());
				File directory = cw.getDir("dat", Context.MODE_PRIVATE);
				File file = new File(directory, "menu.ser");
				FileInputStream f = new FileInputStream(file);
				ObjectInputStream s = new ObjectInputStream(f);
				map = (Map<String, ArrayList<CatalogueDetail>>) s.readObject();
				s.close();
				if (arg0[0].equals("1")) {
					for (String key : map.keySet()) {
						adapter.add(new MenuItem(key, R.drawable.item));
					}
				} else if (arg0[0].equals("2")) {
					for(CatalogueDetail val : map.get(arg0[1])){
						adapter.add(new MenuItem(val.getId(), val.getCtName(), R.drawable.item));
					}
				}
				return adapter;
			}catch(Throwable e){
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(MenuAdapter result) {
			setListAdapter(result);
		}
	}
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MenuAdapter adapter = new MenuAdapter(getActivity());
		index = getArguments().getInt("index");
		
		switch (getArguments().getInt("level")) {
		case 1:
			new MenuAsyncTask().execute("1");
			break;
		case 2:
			new MenuAsyncTask().execute("2", getArguments().getString("key"));
			break;
		}
		View view = getLayoutInflater(savedInstanceState).inflate(
				R.layout.footer_back, null);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				FragmentTransaction t = getFragmentManager().beginTransaction();
				t.setCustomAnimations(android.R.anim.slide_in_left,
						android.R.anim.slide_out_right);
				Fragment fragment = null;
				Bundle bundle = new Bundle();
				switch (getArguments().getInt("level")) {
				case 1:
					fragment = new LeftMenuFragment();
					break;
				case 2:
					fragment = new NestedMenuFragment();
					bundle.putInt("level", 1);
					break;
				}
				fragment.setArguments(bundle);
				t.replace(R.id.menu_frame, fragment);
				t.commit();
				
			}
		});
		getListView().addFooterView(view);
		setListAdapter(adapter);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		MenuItem item = ((MenuItem)getListAdapter().getItem(position));
		if (getArguments().getInt("level") == 1 && map.get(item.tag).size() > 0) {
			FragmentTransaction t = this.getFragmentManager().beginTransaction();
			t.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
			NestedMenuFragment nestedMenuFragment = new NestedMenuFragment();
			Bundle bundle = new Bundle();
			bundle.putInt("level", 2);
			bundle.putInt("index", position);
			bundle.putString("key", item.tag);
			nestedMenuFragment.setArguments(bundle);
			t.replace(R.id.menu_frame, nestedMenuFragment);
			t.commit();
		} else {
			FragmentTransaction f = this.getFragmentManager()
					.beginTransaction();
			ProductListActivity pc = new ProductListActivity();
			Bundle bundle = new Bundle();
			bundle.putString("catalogId", item.id);
			pc.setArguments(bundle);
			f.replace(R.id.main_frame, pc);
			f.commit();
			((MyPointBaseActivity) getActivity()).toggle();
		}
	}
}
